package org.example.cotroller.user;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.PageBean;
import org.example.entity.Result;
import org.example.entity.User;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserController {


//    private static Logger log = LoggerFactory.getLogger(UserController.class);
    @Resource
    private UserService userService;
    @Autowired
    private HttpServletRequest httpServletRequest;
    /**
     * 处理用户请求
     */


    /**
     *查询用户表单
     */

//    @RequestMapping(value = "",method = RequestMethod.GET)
//    public Result list(){//查询全部用户
//        log.info("查询全部数据");
//        List<User> users = userService.list();
//        return Result.success(users);
//    }

    /**
     *根据id删除指定用户
     */

    @GetMapping("/{userId}")
    public Result delete(@PathVariable String userId){
        log.info("删除：{}",userId );
        if (userService.delete(Integer.parseInt(userId)))
            return Result.success();
        else return Result.error(userId +"不存在");
    }
    /**
     *根据id批量删除指定用户
     */
    @GetMapping("/deleteList")
    public Result deletes(String[] userId){
        userService.deleteList(userId);
        return Result.success();
    }
    /**
     *新增用户
     */
    @PostMapping("")
    public Result add(@RequestBody User user){
        log.info("新增用户：{}",user.toString());

        userService.add(user);

        return Result.success();
    }

    /**
     *编辑用户
     */
    @PostMapping("/{userId}")
    public Result update(@RequestBody User user,@PathVariable String userId){
        log.info("修改用户信息");
        System.out.println(user.toString());
        userService.update(user,Integer.parseInt(userId));
        return Result.success();
    }

    /**
     *查询用户
     */
    @GetMapping("")
    public Result pageList(@RequestParam(value = "userId",required = false, defaultValue = "") Integer userId,
                           @RequestParam(value = "username",required = false, defaultValue = "") String username,
                           @RequestParam(value = "name",required = false, defaultValue = "") String name,
                           @RequestParam(value = "gender",required = false, defaultValue = "") String gender,
                           @RequestParam(value = "phone",required = false, defaultValue = "") String phone,
                           @RequestParam(value = "pageSize",required = false, defaultValue = "10") Integer pageSize,
                           @RequestParam(value = "page",required = false, defaultValue = "1")Integer page){
        log.info("查询内容:{},{},{},{}",username,name,gender,phone);
        PageBean userPageBean = userService.userByPage(pageSize,page,userId,username,gender,phone,name);

//        System.out.println(pageSize==null);
//        userPageBean.setUsers(userService.listByPage(pageSize,page));
//        userPage.setUsers(userService.listByPage(Integer.parseInt(pageSize),Integer.parseInt(page)));
//        userPageBean.setTotalPage(userService.listSize());
        return Result.success(userPageBean);
    }


}
